package whu.hami.tea.view;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import whu.hami.tea.R;

/**
 * Created by pendragon on 17-5-27.
 */

public class TitleBar extends LinearLayout {
    private ImageView imgBack;
    private TextView tvTitle;
    private TextView tvAction;
    private ImageView imgAction;

    public TitleBar(Context context) {
        super(context);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void setTitle(String title){
        tvTitle.setText(title);
    }

    public void setBackListener(OnClickListener clickListener){
        imgBack.setOnClickListener(clickListener);
    }

    public void setActionListener(String actionName, OnClickListener onClickListener){
        tvAction.setVisibility(VISIBLE);
        imgAction.setVisibility(GONE);
        tvAction.setText(actionName);
        tvAction.setOnClickListener(onClickListener);
    }

    public void setImageActionListener(OnClickListener onClickListener){
        tvAction.setVisibility(GONE);
        imgAction.setVisibility(VISIBLE);
        imgAction.setOnClickListener(onClickListener);
    }

    private void init(final Context context){
        LayoutInflater.from(context).inflate(R.layout.view_titlebar, this);

        tvTitle = (TextView) findViewById(R.id.tv_title);
        imgBack = (ImageView) findViewById(R.id.img_back);
        imgBack.setClickable(true);
        imgBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((AppCompatActivity)context).finish();
            }
        });

        tvAction = (TextView) findViewById(R.id.tv_action);
        tvAction.setClickable(true);

        imgAction = (ImageView) findViewById(R.id.img_action);
        imgAction.setClickable(true);
    }

}
